//
//  ViewController.m
//  图片浏览器
//
//  Created by student on 16/2/20.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "PicModel.h"

@interface ViewController ()

@property (nonatomic,weak) UILabel *indexLabel;

@property (nonatomic,weak) UIButton *leftBtn;

@property (nonatomic,weak) UIButton *rightBtn;

@property (nonatomic,weak) UIImageView *imageView;

@property (nonatomic,weak) UITextView *textView;

@property (nonatomic,strong) NSArray *dataArr;

@property (nonatomic,assign) int index;

@end

@implementation ViewController

//懒加载数据
-(NSArray *)dataArr{
    if (_dataArr == nil) {
        //1.获取文件路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"imageData" ofType:@"plist"];
        //2.读取数据
        NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
        //3.创建可变数组
        NSMutableArray *mutArr = [NSMutableArray array];
        //4.遍历数据
        for (NSDictionary *dic in tempArr) {
            PicModel *pic = [PicModel PicModelWithDic:dic];
            [mutArr addObject:pic];
        }
        //5.赋值给dataArr
        _dataArr = mutArr;
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     self.index = 1;
    [self setUpUI];
   
}
-(void)setUpUI{
    self.imageView = [self setUpImageViewWithFrame:CGRectMake(100, 100, 214, 200) WithImagename:@"biaoqingdi"];
    self.leftBtn = [self setUpBtnWithFrame:CGRectMake(25, 175, 50, 50) WithImageName:@"left_normal" WithHighLightImageName:@"left_highlighted"];
    self.leftBtn.enabled = NO;
    [self.leftBtn addTarget:self action:@selector(didClickLeftBtn) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn = [self setUpBtnWithFrame:CGRectMake(329, 175, 50, 50) WithImageName:@"right_normal" WithHighLightImageName:@"right_highlighted"];
    [self.rightBtn addTarget:self action:@selector(didClickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    NSString *str = [NSString stringWithFormat:@"%i/%lu",self.index,(unsigned long)self.dataArr.count];
    self.indexLabel = [self setUpLabelWithFrame:CGRectMake(187, 35, 30, 30) WithText:str];
    self.textView = [self setUpTextViewWithFrame:CGRectMake(50, 330, 314, 100) WithText:@"在他面前，其他神马表情都弱爆了！"];
}
//搭建界面中的imageView
-(UIImageView *)setUpImageViewWithFrame:(CGRect)frame
                 WithImagename:(NSString *)image{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    [imageView setImage:[UIImage imageNamed:image]];
    [self.view addSubview:imageView];
    return imageView;
}
//搭建界面中的Btn
-(UIButton *)setUpBtnWithFrame:(CGRect)frame
           WithImageName:(NSString *)image
  WithHighLightImageName:(NSString *)highLight{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:highLight] forState:UIControlStateHighlighted];
    [self.view addSubview:btn];
    return btn;
}
//搭建界面中的Label
-(UILabel *)setUpLabelWithFrame:(CGRect)frame
                  WithText:(NSString *)text{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:text];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:[UIColor blackColor]];
    [self.view addSubview:label];
    return label;
}
//搭建界面中的textView
-(UITextView *)setUpTextViewWithFrame:(CGRect)frame
                     WithText:(NSString *)text{
    UITextView *textView = [[UITextView alloc] initWithFrame:frame];
    [textView setText:text];
    [textView setTextColor:[UIColor blackColor]];
    [textView setTextAlignment:NSTextAlignmentCenter];
    [textView setFont:[UIFont systemFontOfSize:20]];
    [self.view addSubview:textView];
    return textView;
}
//左边按钮的点击事件
-(void)didClickLeftBtn{
    self.index -= 1;
    [self changeImageAndLabelAndText];
}
//右边按钮的点击事件
-(void)didClickRightBtn{
    self.index += 1;
    [self changeImageAndLabelAndText];
}
-(void)changeImageAndLabelAndText{
    [self.indexLabel setText:[NSString stringWithFormat:@"%i/%lu",self.index,(unsigned long)self.dataArr.count]];
    PicModel *pic = self.dataArr[self.index - 1];
    NSString *imageName = pic.icon;
    [self.imageView setImage:[UIImage imageNamed:imageName]];
    [self.textView setText:pic.desc];
    self.rightBtn.enabled = self.index != self.dataArr.count;
    self.leftBtn.enabled = self.index != 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
